#include "head.h"

//插入数据
int insertpayment(Payment * pay){

    //定义sql字符串
    char sql[256];
    //拼接字符串
    sprintf(sql,
            "insert into payment (person_id,price,ynpay) values(%d,%.2f,'%s');",
            pay->person_id,pay->price,pay->ynpay);
    char * errmsg;
    //执行sql语句
    int rc = sqlite3_exec(db,sql,NULL,NULL,&errmsg);
    if(rc!=SQLITE_OK){
        fprintf(stderr,"insert payment : 插入出错 : %s\n",errmsg);
        sqlite3_free(errmsg);
        return -1;
    }
    return 0;
}


//判断有没有该缴费id的信息
int yn_query_pay_personid(int id){

    //定义查询sql，其中参数用?号占位
    const char * sql = "select pay_id as 序号,person_id as 业主编号,price as 需缴费金额,ynpay as 是否缴费成功 from payment  where pay_id= ? ;";

    sqlite3_stmt * stmt;
    int rc = sqlite3_prepare_v2(db,sql,-1,&stmt,NULL);
    //判断预编译是否成功
    if(rc!=SQLITE_OK){
        fprintf(stderr,"预编译出错了:%s\n", sqlite3_errmsg(db));
        sqlite3_finalize(stmt);
        return -1;
    }
    sqlite3_bind_int(stmt,1,id);
    int a=0;
    //执行查询sql语句
    // sqlite3_step(stmt):进行一次读取,会返回当前指向的位置，是否是一个有效的数据行，如果是数据行就可以获取改行数据
    while (sqlite3_step(stmt)==SQLITE_ROW){

        //读取当前指向行的各个列的数据
        int id = sqlite3_column_int(stmt,0);
        int pid = sqlite3_column_int(stmt,1);
        double n = sqlite3_column_double(stmt,2);
        char * yn = sqlite3_column_text(stmt,3);
        a++;
    }
    //释放语句句柄
    sqlite3_finalize(stmt);
    if(a==0){
        return -1;
    }


    return 0;
}


//查询单个业主缴费信息
int query_pay_personid(char * name){

    //定义查询sql，其中参数用?号占位
    const char * sql = "select p.pay_id as 序号,p.person_id 住户编号,p.price 需缴费金额,p.ynpay 是否缴费成功 from payment p join resident res on p.person_id=res.person_id  where res.name= ? ;";

    sqlite3_stmt * stmt;
    int rc = sqlite3_prepare_v2(db,sql,-1,&stmt,NULL);
    //判断预编译是否成功
    if(rc!=SQLITE_OK){
        fprintf(stderr,"预编译出错了:%s\n", sqlite3_errmsg(db));
        return -1;
    }
    sqlite3_bind_text(stmt,1,name,-1,SQLITE_TRANSIENT);
    int a=0;
    //执行查询sql语句
    // sqlite3_step(stmt):进行一次读取,会返回当前指向的位置，是否是一个有效的数据行，如果是数据行就可以获取改行数据
    while (sqlite3_step(stmt)==SQLITE_ROW){

        //读取当前指向行的各个列的数据
        int id = sqlite3_column_int(stmt,0);
        int pid = sqlite3_column_int(stmt,1);
        double n = sqlite3_column_double(stmt,2);
        char * yn = sqlite3_column_text(stmt,3);
        a++;
        printf("缴费单号:%d 住户id:%d 需缴费金额:%.2f 是否缴费成功:%s\n",id,pid,n,yn);
    }
    if(a==0){
        printf("该业主没有需缴费记录。\n");
    }

    //释放语句句柄
    sqlite3_finalize(stmt);
    return 0;
}

 int row2 = 0;
//定义查询需要的回调函数 :
int pay_callback(void * data,int argc,char ** argv,char ** arg_col_name){
    (*(int *)data)++;

    //使用静态变量，只输出一次列标题

    if(row2==0) {
        //遍历列名字
        for (int i = 0; i < argc; i++) {
            printf("|%s ", arg_col_name[i]);
        }
        row2 = 1;
        printf("|\n");
        printf("-------------------------------------------\n");
    }

    //遍历列值
    for(int i=0;i<argc;i++){
        printf("|%s ",argv[i]);
    }
    printf("|\n");
    return 0;
}



//查询所有的缴费数据
int query_pay_all(){
    char * errmsg;
    row2 = 0;
    int rowcount = 0;
    int rc = sqlite3_exec(db,"select p.pay_id as 序号,p.person_id 住户编号,p.price 需缴费金额,p.ynpay 是否缴费成功 from payment p;",pay_callback,&rowcount,&errmsg);
    printf("本次查询的行数：%d\n",rowcount);

    if(rc!=SQLITE_OK){
        fprintf(stderr,"查询出错了:%s",errmsg);
        sqlite3_free(errmsg);
        return -1;

    }

    return 0;
}

//用于更改float类型数据
int update_pay4(int id,float shuju, char * sqlname){
    char sql[256];
    sprintf(sql,"update payment set %s=%f where pay_id=%d;",sqlname,shuju,id);
    int rc = sqlite3_exec(db,sql,NULL,NULL,NULL);
    if(rc!=SQLITE_OK){
        return -1;
    }
    return 0;
}


//用于更改int类型内容
int update_pay2(int id,int shuju,char * sqlname){
    char sql[250];
    sprintf(sql,"update payment set %s=%d where pay_id=%d;",sqlname,shuju,id);
    int ls=sqlite3_exec(db,sql,NULL,NULL,NULL);
    if (ls!=SQLITE_OK){
        return -1;
    }
    return 0;
}

//用于更改text类型内容
int update_pay3(int id,char * shuju,char * sqlname){
    char sql[250];
    sprintf(sql,"update payment set %s='%s' where pay_id=%d;",sqlname,shuju,id);
    int ls=sqlite3_exec(db,sql,NULL,NULL,NULL);
    if (ls!=SQLITE_OK){
        return -1;
    }
    return 0;
}




//修改特定数据
void update_pay(int id){
    int a;//用于导航选择
    int b=1;//循环条件
    int c=0;//避免重复提醒
    int x;//存放新改的内容
    char d[50];//存放新改的内容
    float l;//存放新改的内容
    int e;
    printf("----------------请选择要更改缴费单为:%d的什么内容----------------\n",id);
    printf("1.更改住户id 2.更改缴费金额 3.更改是否已缴费 4.退出\n");
    scanf("%d",&a);
    while(b){
        if(c!=0){
            printf("----------------选择继续对缴费单为:%d更改的内容----------------\n",id);
            printf("1.更改住户id 2.更改缴费金额 3.更改是否已缴费 4.退出\n");
            scanf("%d",&a);
        }
        c++;
        switch (a) {
            case 1:
                printf("请输入新住户id:\n");
                scanf("%d",&x);
                e=update_pay2(id,x,"person_id");
                if(e==-1){
                    printf("更改失败\n");
                }else{
                    printf("更改成功！\n");
                }
                break;
            case 2:
                printf("请输入新的缴费金额:\n");
                scanf("%f",&l);
                e= update_pay4(id,l,"price");
                if(e==-1){
                    printf("更改失败\n");
                }else{
                    printf("更改成功！\n");
                }
                break;
            case 3:
                printf("请输入是否已经缴费成功:\n");
                scanf("%s",d);
                while (1){
                    if (strcmp(d, "是") == 0 || strcmp(d, "否") == 0) {
                        break;
                    } else {
                        printf("请输入是或否：\n");
                        scanf("%s", d);
                    }
                }
                e=update_pay3(id,d,"ynpay");
                if(e==-1){
                    printf("更改失败\n");
                }else{
                    printf("更改成功！\n");
                }
                break;
            case 4:
                b=0;
                break;
            default:
                printf("输入错误请重新输入:\n");
                break;
        }
    }
}



//删除数据
int delete_pay(int id){
    //定义sql字符串
    char sql[256];
    //拼接字符串
    sprintf(sql,
            "delete from payment  where pay_id = %d; ",
            id);
    char * errmsg;
    int rc = sqlite3_exec(db,sql,NULL,NULL,&errmsg);
    if(rc!=SQLITE_OK){
        fprintf(stderr,"delete payment : 删除出错 : %s\n",errmsg);
        sqlite3_free(errmsg);
        return -1;
    }
    return 0;
}

void payment_main() {

    Payment pay;
    int b,c,d=1,e=0;
    int h;//删除数据的id
    int p;//存repair_id
    int m=1;//判断是否可以跳出case2，3的循环
    printf("1.添加缴费信息 2.修改缴费信息 3. 删除缴费信息 4.查询缴费信息 5.退出\n");
    c= hcq(c);
    while (d) {
        if (e != 0) {
        printf("1.添加缴费信息 2.修改缴费信息 3. 删除缴费信息 4.查询缴费信息 5.退出\n");
        c= hcq(c);
        }
        e++;
        switch (c) {
            case 1:
                //插入数据
                printf("请输入住户id：");
                scanf("%d", &pay.person_id);
                printf("请输入续缴费金额：");
                scanf("%f", &pay.price);
                printf("请输入是否缴费成功：");
                scanf("%s", pay.ynpay);
                while (1){
                    if (strcmp(pay.ynpay, "是") == 0 || strcmp(pay.ynpay, "否") == 0) {
                        break;
                    } else {
                        printf("请输入是或否：\n");
                        scanf("%s", pay.ynpay);
                    }
                }
                insertpayment(&pay);
                printf("插入成功！！！\n");
                break;
            case 2:
                //修改数据
                m=1;
                while(m){
                    printf("请输入要修改的缴费单id:\n");
                    scanf("%d",&p);
                    if(yn_query_pay_personid(p)==0){
                        update_pay(p);
                        m=0;
                        break;
                    }
                    else{
                        printf("输入的缴费单id不存在！！！\n");
                    }

                }
                break;



            case 3:
                m=1;
                while(m){
                    printf("请输入要删除缴费单的id：");
                    scanf("%d", &h);
                    if(yn_query_pay_personid(h)==0) {
                        delete_pay(h);
                        m=0;
                        printf("删除成功！！！\n");
                        break;
                    }
                else{
                    printf("输入的缴费单id不存在！！！\n");
                }
            }
                break;


            case 4:
                //查询数据
                printf("1.查询所有缴费单数据  2.查询xxx业主缴费数据\n");
                int x;
                scanf("%d",&x);
                while (1){
                    if(x==1){
                        query_pay_all();
                        break;
                    }
                    else if(x==2){
                        printf("输入业主姓名：");
                        char xx[19];
                        scanf("%s",xx);
                        query_pay_personid(xx);
                        break;
                    }
                    else{
                        scanf("%d",&x);
                    }
                }
                break;
            case 5:
                d = 0;
                break;
            default:
                break;
        }


    }
}
